﻿//04.Write a program that finds how many times a substring is contained in a given text (perform case insensitive search).
//Example: The target substring is "in". The text is as follows:
//We are living in an yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days.
//The result is: 8.

using System;

namespace FindSubstring
{
    class FindSubstring
    {
        static void Main()
        {
            Console.Write("Enter the target substring: ");
            string target = Console.ReadLine();
            target.ToLower();

            Console.Write("Enter the text: ");
            string text = Console.ReadLine();
            text.ToLower();

            Console.WriteLine();
            Console.WriteLine("\"{0}\" appears {1} times.", target, Occurrence(text, target));
        }

        private static int Occurrence(string text, string target)
           {
            int occurrence = 0;

            while (text.IndexOf(target) >= 0)
            {
                int position = text.LastIndexOf(target);
                if (text.Substring(position, target.Length) != "")
                {
                    occurrence++;
                    text = text.Remove(position);
                }
            }
            return occurrence;
           }
        }
    }
